﻿var timer;
var timeRun = 0;
var imageURL = "";
var isDocked = true;
function view_onOpen() {
InitializeStoredOptions();
pluginHelper.onDisplayTargetChange = onDisplayTargetChange;
imageURL = ReturnFormattedURL() ;
RefreshImage();
refreshRate = options.getValue ('RefreshRate');
timer = view.setInterval (RefreshImage, refreshRate);
if (isDocked) {
  view.resizable = 'zoom';
} else {
  view.resizable = 'true';
}
}

function RefreshImage()
{

debug.info ("Inside Refresh Image, URL:" +  imageURL + ", refresh:" + options.getValue ('RefreshRate') + "resolution:" + options.getValue('Resolution'));
ajaxPicture(imageURL, ShowImage);
timeRun++;
debug.warning (timeRun);
}

function ShowImage(responseStream)
{
debug.info ("Showing image");
img1.src = responseStream;
if (view.isDocked)
{
img1.height = img1.srcHeight/20;
}
else
{
img1.width = img1.srcWidth;
img1.height = img1.srcHeight;
}
view.resizeTo (img1.width, img1.height);
}

function ajaxPicture(url, callback)
{
  var request = new XMLHttpRequest();
 request.onreadystatechange = onReadyStateChange;
 function onRequestError() {
    if (request) {
      request.abort();
      // Prevent leak.
      request = null;
    }
	debug.warning('Request error, will retry later.');
    // Retry again later.
     view.setTimeout(RefreshImage, options.getValue('RefreshRate'));
}



try {
request.open("GET", url, true); 
  request.send(null);
  } catch(e) 
			{
			debug.warning ("Could not retrieve image: " + e.message);
      onRequestError();
			}
   


 function onReadyStateChange() {
    if (request.readyState != 4) {
      return;
    }

    if (request.status == 200) {
/*      if (timeoutTimer) {
        debug.trace('Cancelling request timeout timer.');
        view.clearTimeout(timeoutTimer);
      } */

      debug.trace('Retrieve succeeded.');
      callback(request.responseStream);
    }
    // Prevent leak.
    request = null;
  }
}

function ReturnFormattedURL ()
{
//debug.info ('Inside ReturnFormattedURL');
var url = "";
debug.trace ("Flip image:" +  options.getValue('FlipImage'));
var rotate =  options.getValue('FlipImage')?180:0;
url = url + "http://" + options.getValue ('URL') + "/axis-cgi/jpg/image.cgi?resolution=" + options.getValue('Resolution') + "&compression=0&color=1&clock=1&date=0&text=1&textpos=top&rotation=" + rotate;
return url;
}

function img1_onclick() {

}

function view_onoptionchanged() {
	debug.Trace ("In Options Changed Event");
	debug.Trace ("Clearing interval, originalRefreshRate:" + refreshRate);
	refreshRate = options.getValue ('RefreshRate');
	debug.Trace ("New interval, refreshRate:" + refreshRate);
		if (options.getValue('LastChange'))
		{
		view.clearInterval (timer);
		imageURL = ReturnFormattedURL();
		RefreshImage();
		timer = view.setInterval (RefreshImage, refreshRate);
		options.putValue ('LastChange', false)
		}
}


function InitializeStoredOptions() {
  options.putDefaultValue('URL', '128.173.201.214');
  options.putDefaultValue('Resolution', '640x480');
  options.putDefaultValue('RefreshRate', 5000);
options.putDefaultValue('FlipImage', false);
options.putDefaultValue('LastChange', false);
}

function onDisplayTargetChange(displayTarget) {
  // Find out the new display mode
  if (displayTarget == gddTargetSidebar) {
    debug.trace('Display target changed to docked');
    isDocked = true;
  } else if (displayTarget == gddTargetFloatingView) {
    debug.trace('Display target changed to undocked');
    isDocked = false;
  }
}

